using System;
using System.Collections.Specialized;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using Impact.Views;

namespace Impact.Web.Foundation
{
    /// <summary>
    /// base class for all pages in the system
    /// </summary>
    public abstract class PageBase : Page, IPageView
    {


        /// <summary>
        /// default system home for the administration section
        /// </summary>
        public const string AdminHome = "Default.aspx";
        /// <summary>
        /// default system home for the front end section
        /// </summary>
        public const string Home = "Default.aspx";

        /// <summary>
        /// override of onload to perform startup processing.  calls Bind() on non-postbacks.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad( EventArgs e )
        {

            base.OnLoad( e );

            // set referring url
            if ( Request.UrlReferrer != null && !Request.Url.Equals( Request.UrlReferrer ) )
            {
                ReferringUrl = Request.UrlReferrer.ToString();
            }

            if ( !IsPostBack )
            {
                Bind();
            }


        }

        /// <summary>
        /// when overridden, this method is used to bind controls to their datasources when a 
        /// page loads and when data changes in the course of a postback.  
        /// this method is called automatically by OnLoad when a page is first loaded, and 
        /// must be called by user code on any postback (after appropriate processing)
        /// </summary>
        protected virtual void Bind()
        {
        }


        #region PageBase Properties

        /// <summary>
        /// the URL that referred the user to this page, not including postbacks
        /// </summary>
        protected string ReferringUrl
        {
            get
            {
                return (string)ViewState["ReferringUrl"];
            }
            set
            {
                ViewState["ReferringUrl"] = value;
            }
        }

        /// <summary>
        /// Gets the currently logged in user's provider key (as a Guid)
        /// </summary>
        public Guid UserId
        {
            get
            {
                MembershipUser user = Membership.GetUser();
                Guid output = Guid.Empty;
                if ( user != null )
                {
                    output = (Guid)user.ProviderUserKey;
                }
                return output;
            }
        }

        #endregion

        #region IPageView Members

        /// <summary>
        /// Gets the parameters passed on the querystring of the current request
        /// </summary>
        public NameValueCollection Parameters
        {
            get { return Request.QueryString; }
        }

        /// <summary>
        /// end the response and forward to the parameter url
        /// </summary>
        public void Redirect( string targetUrl )
        {
            Response.Redirect( targetUrl, false );
        }

        /// <summary>
        /// redirects the user back to the referring url
        /// </summary>
        public void Return()
        {
            Redirect( ReferringUrl ?? "default.aspx" );
        }

        /// <summary>
        /// displays an error message on the view
        /// </summary>
        public void ShowErrorMessage( string message )
        {
            HtmlGenericControl label = new HtmlGenericControl( "div" );
            label.InnerHtml = "ERROR: " + message;
            label.Style.Add( HtmlTextWriterStyle.Color, "Red" );

            Control body = this.Master.FindControl( "Body" );
            if ( body != null )
            {
                body.Controls.Add( label );
            }
            else
            {
                this.Controls.Add( label );
            }
        }

        /// <summary>
        /// the client IP address of the user
        /// </summary>
        public string UserHostAddress
        {
            get { return Request.UserHostAddress; }
        }

        #endregion


    }



}
